$(function(){
  //获取
  $("#btnone1").on("click", function () {
      $("#myfile").click();
  })
  
  //上传图片
  $("#myfile").on("change", function (e) {
      let fd = new FormData();
      $.each(e.target.files, function (index, item) {
          fd.append('swipers', item)
      });
      $.ajax({
          type: "POST",
          url: "http://localhost:8888/admin/swipers",
          processData: false,
          contentType: false,
          beforeSend: function (xhr) {
            xhr.setRequestHeader("Authorization", sessionStorage.getItem('mytoken'))
        },
          data: fd,
          success: function (response) {
              loadSwi()
          }
      });

  })


  function loadSwi() {
      $.ajax({
          type: "get",
          url: "http://localhost:8888/admin/swipers",
          beforeSend: function (xhr) {
            xhr.setRequestHeader("Authorization", sessionStorage.getItem('mytoken'))
        },
          success: function (a) {
              console.log(a);
              $("tbody").html("")
              var str = '';
              a.data.forEach(function(item) {
                  str += `<tr>
                          <th>${item.id}</th>
                          <th><img src="http://localhost:8888/uploads/${item.swiperimg}"></th>
                          <th>${item.swiperimg}</th>
                          <th status="${item.swiperstatus}" data-id="${item.id}">${item.swiperstatus == 1 ? '<span data-id="1" class="layui-badge layui-bg-green">√</span> ' : '<span data-id="2" class="layui-badge layui-bg-cyan">×</span> '}</th>
                          <th><button data-id="${item.id}" type="button" class="layui-btn layui-btn-xs layui-btn-danger delete">
                          删除
                          </button></th>
                          </tr>`

              });
              $("tbody").html(str)
          }
      });
  }

  loadSwi()

  //启动禁用图

  $("tbody").on("click", ".layui-badge", function () {
      let id = $(this).parent("th").attr("data-id");
      let status = 1;
      $(this).attr("data-id") == "2" ? status = 1 : status = 2;
      console.log({
          status
      });
      $.ajax({
          type: "put",
          url: "http://localhost:8888/admin/swipers/" + id,
          data: {
              status
          },
          beforeSend: function (xhr) {
            xhr.setRequestHeader("Authorization", sessionStorage.getItem('mytoken'))
        },
          success: function (response) {
              console.log(response);
              loadSwi()
          }
      });

  })

  //删除
  $('.layui-table tbody').on('click', '.delete', function (e) {
      var id = $(this).attr("data-id")
      layer.confirm('确实要删除吗？', function (index) {
          $.ajax({
              type: 'delete',
              url: 'http://localhost:8888/admin/swipers/' + id,
              beforeSend: function (xhr) {
                xhr.setRequestHeader("Authorization", sessionStorage.getItem('mytoken'))
            },
              success: function (res) {
                  if (res.status === 0) {
                      // 删除成功
                      layer.close(index)
                      loadSwi()
                  }
              }
          })
      })
  })

})
